package com.bilubi.reggie.reggie.service.Impl;

import com.bilubi.reggie.reggie.bean.Dish;
import com.bilubi.reggie.reggie.bean.DishFlavor;
import com.bilubi.reggie.reggie.bean.vo.DishVo;
import com.bilubi.reggie.reggie.mapper.DishFlavorMapper;
import com.bilubi.reggie.reggie.mapper.DishMapper;
import com.bilubi.reggie.reggie.service.DishService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class DishServiceImpl implements DishService {

    private static final Logger log = LoggerFactory.getLogger(DishServiceImpl.class);
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Map<String,Object>> getAllDish(String name) {
        return dishMapper.selectAllDish(name);
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public Integer addDish(DishVo dishVo) {
        int count1 = dishMapper.insertSelective(dishVo);
        List<DishFlavor> flavors = new ArrayList<>();
        dishVo.getFlavors().forEach(flavor -> {
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setName(flavor.getName());
            dishFlavor.setValue(flavor.getValue());
            dishFlavor.setDishId(dishVo.getId());
            flavors.add(dishFlavor);
        });
        int count2 = dishFlavorMapper.insert(flavors);
        if(count1 > 0 && count2 == flavors.size()){
            return 1;
        }
        else
            return 0;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public Integer updateDish(DishVo dishVo) {
        // 更新dish
        dishMapper.updateByPrimaryKeySelective(dishVo);
        // 删除dish旧flavor
        dishFlavorMapper.deleteByDishId(dishVo.getId());
        // 增加dish新flavor
        List<DishFlavor> flavors = new ArrayList<>();
        dishVo.getFlavors().forEach(flavor -> {
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setName(flavor.getName());
            dishFlavor.setValue(flavor.getValue());
            dishFlavor.setDishId(dishVo.getId());
            flavors.add(dishFlavor);
        });
        return dishFlavorMapper.insert(flavors);
    }

    @Override
    public Integer deleteDishById(String ids) {
        Long[] ids_ = Arrays.stream(ids.split(",")).map(Long::parseLong).toArray(Long[]::new);
        List<Dish> list = new ArrayList<>();
        Arrays.stream(ids_).forEach(x->{
            Dish dish = new Dish();
            dish.setId(x);
            dish.setIsDeleted(1);
            list.add(dish);
        });
        return dishMapper.deleteById(list);
    }

    @Override
    public DishVo getDishById(Long id) {
        DishVo dishVo = dishMapper.selectByPrimaryKey(id);
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        dishVo.setFlavors(flavors);
        return dishVo;
    }

    @Override
    public Integer changeStatusById(Integer status, String ids) {
        Long[] ids_ = Arrays.stream(ids.split(",")).map(Long::parseLong).toArray(Long[]::new);
        List<Dish> list = new ArrayList<>();
        Arrays.stream(ids_).forEach(x->{
            Dish dish = new Dish();
            dish.setId(x);
            dish.setStatus(status);
            list.add(dish);
        });
        return dishMapper.updateStatusById(list);
    }

    @Override
    public List<DishVo> getDishByCategoryId(Long categoryId) {
        // 构建redis数据库key
        String key = "dish_" + categoryId;
        // 先在redis数据库查数据
        List<DishVo> dishVos = (List<DishVo>) redisTemplate.opsForValue().get(key);
        if(dishVos == null){
            // 若redis数据库无数据，再去mysql查询，并将结果存在redis中
            dishVos = dishMapper.selectDishByCategoryId(categoryId);
            redisTemplate.opsForValue().set(key, dishVos, 60, TimeUnit.MINUTES);
        }
        return dishVos;
    }
}
